package co.yixiang.modules.system.rest;

import lombok.AllArgsConstructor;
import cn.hutool.core.convert.Convert;
import co.yixiang.logging.aop.log.Log;
import co.yixiang.modules.system.domain.Transfer;
import co.yixiang.modules.system.service.TransferService;
import co.yixiang.modules.system.dto.queryCriteria.TransferQueryCriteria;
import co.yixiang.modules.system.dto.TransferDto;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* @author db
* @date 2021-07-10
*/
@AllArgsConstructor
@Api(tags = "过户价管理")
@RestController
@RequestMapping("/api/transfer")
public class TransferController {

    private final TransferService transferService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('admin','transfer:list')")
    public void download(HttpServletResponse response, TransferQueryCriteria criteria) throws IOException {
        transferService.download(Convert.toList(TransferDto.class, transferService.queryAll(criteria)), response);
    }

    @GetMapping
    @Log("查询过户价")
    @ApiOperation("查询过户价")
    @PreAuthorize("@el.check('admin','transfer:list')")
    public ResponseEntity<Object> getTransfers(TransferQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(transferService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增过户价")
    @ApiOperation("新增过户价")
    @PreAuthorize("@el.check('admin','transfer:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody Transfer resources){
        return new ResponseEntity<>(transferService.save(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改过户价")
    @ApiOperation("修改过户价")
    @PreAuthorize("@el.check('admin','transfer:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody Transfer resources){
        transferService.updateById(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除过户价")
    @ApiOperation("删除过户价")
    @PreAuthorize("@el.check('admin','transfer:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody List<Integer> ids) {
        transferService.removeByIds(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}